package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.mkt.BidSecurityDto;
import top.infopub.dto.mkt.TenderPlanningDto;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.BidSecurityService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.mkt.BidBondApply;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

/**
 * @Author Warking
 * @Date 2021/5/2 20:40
 * @Version 1.0
 */
@Controller
@RequestMapping("/bidSecurity")
public class BidSecurityController extends GenericFlowController {
    @Autowired
    private BidSecurityService bidSecurityService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Autowired
    private OAFlowService oaFlowService;

    //投标保证金申请详情页面
    @RequestMapping("/queryDetail")
    @ResponseBody
    public BidSecurityDto queryDetail(String proCode,String bondType) {
        BidSecurityDto dto = bidSecurityService.queryDetail(proCode,bondType);
        return dto;
    }


    //投标保证金保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bidSecurityDto, String identifying, String proCode) {
        if (StringUtils.isEmpty(bidSecurityDto) || StringUtils.isEmpty(proCode) || StringUtils.isEmpty(identifying)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }
        String object = StringEscapeUtils.unescapeJava(bidSecurityDto);
        BidSecurityDto dto = JSON.parseObject(object, BidSecurityDto.class);
        if ("0".equals(identifying)) {//0是保存
            int i = bidSecurityService.save(dto, proCode);
            if (i > 0) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else { //1是提交
            //判断标书策划的状态
            BidBondApply applyFront = dto.getBidBondApply();
            BidBondApply bidBondApply = bidSecurityService.selectByProCode(proCode,applyFront.getBondType());


            if ("1".equals(bidBondApply.getProcessStatus()) || "9".equals(bidBondApply.getProcessStatus())) {
                return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
            }
            //提交流程  先保存
            int i = bidSecurityService.save(dto, proCode);
            if (i > 0) {//保存成的情况 走流程
                //根据唯一procode查询业务表SGUID

                if (StringUtils.isNotBlank(bidBondApply.getOaId())) {
                    oaFlowService.finishOA(bidBondApply.getOaId());
                }
                //登录人员信息
                AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
                //发起流程
                String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE8, object, bidBondApply.getId(), proCode, vo);
                if (!StringUtils.isEmpty(process)) {
                    return RspVo.getSuccessResponse(0, "提交成功");
                }
                return RspVo.getFailureResponse(0, "提交失败");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }

        }
    }
}

























